package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetShockSetting;
import com.jt.tenant.widget.MySeekBar;
import com.jt.tenant.widget.OnOffView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.view.CheckView;

/**
 * Created by yzf on 2016/2/15.
 */
public class ShockSettingActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name,shock_set;

    private MySeekBar seekbar;

    private int mProgress;

    private TextView shock_num1, shock_num2;

    private static ChangeDateListener changeDateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_shock_setting);

        init_view();

        changeDateListener = new ChangeDateListener() {

            @Override
            public void changeDate(int pro) {

                shock_num1.setText(pro + "");

            }

        };
//
//        if (JTApplication.jtPreferences.readShockIsFirst()) {
//            //第一次进入
//            shock_num1.setText("50");
//
//            shock_num2.setText("当车辆受到力度50以上的震动时发送提醒");
//
//            seekbar.setSeekBarText("50");
//
//            seekbar.setProgress(50);
//
//            JTApplication.jtPreferences.saveShockIsFirst(false);
//
//            //走接口
//            new FirstShockSetting(new AsyCallBack<FirstShockSetting.Info>()).execute(this);
//
//        } else {
            //第2次进入  读取本地数据
            int num = JTApplication.jtPreferences.readShockNum();

            shock_num1.setText(num + "");

            shock_num2.setText("当车辆受到力度" + num + "以上的震动时发送提醒");

            seekbar.setSeekBarText(num + "");

            seekbar.setProgress(num);


    }

    private void init_view() {

        shock_num1 = (TextView) findViewById(R.id.tv_shock_num1);

        shock_num2 = (TextView) findViewById(R.id.tv_shock_num2);

        (shock_set = (TextView)findViewById(R.id.tv_shock_set) ).setOnClickListener(this);

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("震动设置");

        seekbar = (MySeekBar) findViewById(R.id.seekBar);

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub

                if(progress == 0){

                    mProgress = 1;

                }else {

                    mProgress = progress;

                }

                seekbar.setSeekBarText("" + mProgress);

                shock_num2.setText("当车辆受到力度" + mProgress + "以上的震动时发送提醒");

                if (ShockSettingActivity.changeDateListener != null) {

                    ShockSettingActivity.changeDateListener.changeDate(mProgress);

                }

            }

        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.tv_shock_set:

                //0开 1关
                new SetShockSetting(mProgress, JTApplication.jtPreferences.readShockBool(), new AsyCallBack<SetShockSetting.Info>() {

                    @Override
                    public void onSuccess(String toast, int type, SetShockSetting.Info info) throws Exception {

                        if (info.msg.equals("000")) {

                            Toast.makeText(context, "震动设置成功！", Toast.LENGTH_SHORT).show();

                            JTApplication.jtPreferences.saveShockNum(mProgress);

                            finish();
                        } else {

                            Toast.makeText(context, info.msg, Toast.LENGTH_SHORT).show();
                        }

                    }
                }).execute(this);

                break;
        }
    }

    public abstract class ChangeDateListener {

        public void changeDate(int pro) {

        }

    }

}
